package com.deep.deepenterprise.api.interceptor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xyz
 * @version 1.0
 * @title ConstraintViolationExceptionHandler
 * @description
 * @create 2024/4/24 20:38
 */
public class DrViolationExceptionHandler {
    private static final Pattern INDEX_PATTERN = Pattern.compile("\\[(\\d+)]");

    /**
     * @return int
     * @Auth xyz
     * @Desc //TODO 返回[数字]的索引，如果有多个，则只返回第一个
     * @Time 20:45 2024/4/24
     * @Param [propertyPath]
     */
    public static int getIndexFromViolation(String propertyPath) {
        Matcher matcher = INDEX_PATTERN.matcher(propertyPath);
        if (matcher.find()) {
            String indexStr = matcher.group(1);
            return Integer.parseInt(indexStr);
        }
        // 如果找不到索引信息，则返回 -1 或其他合适的默认值
        return -1;
    }
}
